# Generate Figure 6

# initialize matrices of vector shares
lmaps2d90s <- matrix(rep(0,24),nrow = 12, ncol = 2)
lmaps2d25s <- matrix(rep(0,24),nrow = 12, ncol = 2)

# average impute results in each year
for (y in 1:12){
  lmaps2d90s[y,] <- Reduce("+",lapply(1:5, function(k){agg[[y]][[k]]$shares}))[,1]/5

  lmaps2d25s[y,] <- Reduce("+",lapply(1:5, function(k){agg[[y]][[k]]$shares}))[,2]/5
}

years <- seq(1989,2022,by = 3)
sz <- 1

lr <- data.frame(years,lmaps2d90s[,1],lmaps2d90s[,2],lmaps2d25s[,1],lmaps2d25s[,2])
colnames(lr) <- c("Year","squarei90","squarew90","squarei25","squarew25")
lr <- reshape2::melt(lr, id.var = c("Year"))

shares <- ggplot(data=lr,aes(x=Year,y=value,colour = variable,pch = variable)) + 
  geom_line(size = sz) + 
  geom_point(size = 4.5) +
  scale_color_manual(name = "Share of Bottom", 
                     labels = c("Income 90%", "Wealth 90%","Income 25%", "Wealth 25%"), 
                     values=c("#994455","#004488","#ee99aa","#6699cc")) +
  scale_shape_manual(name = "Share of Bottom", 
                     labels = c("Income 90%", "Wealth 90%","Income 25%", "Wealth 25%"), 
                     values=c(17,15,17,15)) +
  theme(plot.title = element_text(hjust = 0.5)) + xlab("Year")+ ylab("Income & Wealth Share")+ 
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.6),
        panel.background = element_rect(fill = 'transparent'),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(color = "gray"),
        panel.grid.minor.y = element_line(color = "gray"),
        legend.background = element_blank(),
        legend.key = element_rect(fill = 'transparent'),
        text = element_text(family = "serif",size=22)) +
  scale_x_continuous(breaks = years[years%%2==0],labels = years[years%%2==0])
ggsave(plot = shares, width = 8, height = 6, filename = "fig6.png")




